package com.xindaoapp.happypet.fragments;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.xindao.photonics.fragments.manager.BaseFragment;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.utils.LoginManager;
import com.xindaoapp.happypet.utils.MoccaPreferences;
import com.xindaoapp.happypet.utils.ProgressHUD;

/**
 * 登录<br/>
 * 
 * 使用方法<br/>
 * Intent intent = new Intent(context, com.xindaoapp.happypet.activity.MainActivity.class);<br/>
 * intent.putExtra(Constants.KEY_SKIP_CLASS_NAME, LoginFragment.class.getCanonicalName());<br/>
 * startActivity(intent);<br/>
 * 
 * @author chenghao
 * 
 */
public class LoginFragment extends BaseFragment implements OnClickListener {
    private EditText vUserName;
    private EditText vPassword;
    private View vLogin;
    private View vQQLogin;
    private View vWeiboLogin;

    private LoginManager mLoginManager;

    @Override
    protected void initEvents(View view) {
        super.initEvents(view);
        vLogin.setOnClickListener(this);
        vQQLogin.setOnClickListener(this);
        vWeiboLogin.setOnClickListener(this);
        view.findViewById(R.id.forget_password).setOnClickListener(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_login, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (mLoginManager == null)
            mLoginManager = new LoginManager(getActivity());
        if (!TextUtils.isEmpty(MoccaPreferences.UserName.get())
                && !TextUtils.isEmpty(MoccaPreferences.PassWord.get())) {
            vUserName.setText(MoccaPreferences.UserName.get());
            vPassword.setText(MoccaPreferences.PassWord.get());
        }
    }

    @Override
    protected void initViews(View view) {
        vUserName = (EditText) view.findViewById(R.id.username);
        vPassword = (EditText) view.findViewById(R.id.password);
        vLogin = view.findViewById(R.id.login);
        vQQLogin = view.findViewById(R.id.login_qq);
        vWeiboLogin = view.findViewById(R.id.login_sina);
    }

    private boolean isInputed() {
        if (TextUtils.isEmpty(vUserName.getText().toString())) {
            Toast.makeText(getActivity(), R.string.input_username_null, Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(vPassword.getText().toString())) {
            Toast.makeText(getActivity(), R.string.input_password_null, Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                if (isInputed()) {
                    mLoginManager.login(
                            ProgressHUD.show(getActivity(), "正在登录请稍候...", true, true, null),
                            vUserName.getText().toString(), vPassword.getText().toString());
                }
                break;
            case R.id.login_qq:
                mLoginManager.loginQQ(true);
                break;
            case R.id.login_sina:
                mLoginManager.loginSina(true);
                break;
            case R.id.forget_password:
                startFragment(new ForgetPasswordFragment());
                break;
            default:
                break;
        }
    }

    @Override
    protected void setRightViewListener(View view) {
        super.setRightViewListener(view);
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 去掉注册功能
                // startFragment(new RegisterActivity());
            }
        });
    }

    @Override
    protected String getTitleName() {
        return "登录";
    }

}
